package com.example.myproject.service.impl;

import com.example.myproject.mapper.UserMoneyMapper;
import com.example.myproject.model.UserMoney;
import com.example.myproject.service.UserMoneyService;
import org.springframework.stereotype.Service;


/**
 * @author 话来话去
 */
@Service
public class UserMoneyImpl implements UserMoneyService {

    private final UserMoneyMapper userMoneyMapper;

    public UserMoneyImpl(UserMoneyMapper userMoneyMapper) {
        this.userMoneyMapper = userMoneyMapper;
    }

    public void reward(Long userId, double point, double ticket, double magicvalue) {
        UserMoney userMoney = userMoneyMapper.selectByUserId(userId);
        if (userMoney == null) {
            // 用户没有记录，初始化一条
            userMoney = new UserMoney();
            userMoney.setUserId(userId);
            userMoney.setPoint(point);
            userMoney.setTicket(ticket);
            userMoney.setMagicvalue(magicvalue);
            userMoneyMapper.insert(userMoney);
        } else {
            // 用户已有记录，更新三项
            userMoneyMapper.addRewards(userId, point, ticket, magicvalue);
        }
    }

    // 查询用户积分
    @Override
    public double getPoint(Long userId) {
        UserMoney userMoney = userMoneyMapper.selectByUserId(userId);
        return userMoney != null ? userMoney.getPoint() : 0.0;
    }

    // 查询用户点券
    @Override
    public double getTicket(Long userId) {
        UserMoney userMoney = userMoneyMapper.selectByUserId(userId);
        return userMoney != null ? userMoney.getTicket() : 0.0;
    }

    // 查询用户魔力值
    @Override
    public double getMagicValue(Long userId) {
        UserMoney userMoney = userMoneyMapper.selectByUserId(userId);
        return userMoney != null ? userMoney.getMagicvalue() : 0.0;
    }
}
